var express = require('express');
var router = express.Router();

var pool = require('../config');

/* GET home page. */
router.get('/', (req, res, next) => {
  res.redirect('/swagger')
});

/**
 * This function comment is parsed by doctrine
 * @route GET /get
 * @group test - Operations about user
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/get', (req, res, next) => {
  console.log(req.headers)
  res.json({
    title: "index",
    message: 'Express'
  });
});

/**
 * This function comment is parsed by doctrine
 * @route POST /post
 * @group test - Operations about user
 * @param {object} file.body - eg: user@domain
 * @param {Point.model} point.body.required - the new point
 * @param {string} email.query.required - username or email - eg: user@domain
 * @param {string} password.query.required - user's password. 
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/post', (req, res, next) => {
  console.log(req.query, req.body, req.file);
  res.json({
    title: "index",
    message: 'Express',
    data: req.query
  });
});

//sql查询
/**
 * 根据密码查询用户
 * @route GET /name
 * @group test - Operations about user 
 * @param {string} password.query.required - 用户's password. 
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.get('/name', function (req, res) {
  let query = "select * from user where password = ? order by id desc";
  let reqData = req.query;
  console.log(req.query, req.body);
  pool.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(query, reqData.password, function (err, resault) {
      if (err) throw err;

      res.send({
        results: resault
      })
    });
    //回收pool
    connection.release();
  });
});
/**
 * JSON parameters require a model. This one just has "name"
 * @typedef ReqNameJSON
 * @property {string} username - name of person making request
 * @property {string} password - password of person making request
 */
/**
 * This function comment is parsed by doctrine
 * @route POST /names
 * @summary POST方法
 * @group names - Operations about user
 * @param {ReqNameJSON.model} user.body.required - username or email
 * @param {string} password.query.required - user's password. 
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 */
router.post('/names', function (req, res, next) {
  console.log(req.query, req.body, req.file);
  res.json({
    title: "index",
    message: 'Express',
    data: {
      query: req.query,
      body: req.body
    }
  });
});

/**
 * This function comment is parsed by doctrine
 * @route POST /file/:name
 * @summary 发送文件
 * @group names - Operations about user
 * @param {string} name.params.required - params.name.
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/file/:name', function (req, res, next) {

  var options = {
    root: path.join(__dirname, 'public'),
    dotfiles: 'deny',
    headers: {
      'x-timestamp': Date.now(),
      'x-sent': true
    }
  }

  var fileName = req.params.name
  res.sendFile(fileName, options, function (err) {
    if (err) {
      next(err)
    } else {
      console.log('Sent:', fileName)
    }
  })
})


module.exports = router;